<?php

require_once dirname(__FILE__).'/../lib/licenseeGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/licenseeGeneratorHelper.class.php';

/**
 * licensee actions.
 *
 * @package    cpfaizenay
 * @subpackage licensee
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class licenseeActions extends autoLicenseeActions
{
    private $titleBC = 'Licenciés';
    private $actionNewBC = 'Nouveau';
    private $actionEditBC = 'Modifier';
    private $actionImportBC = 'Importer fichier csv';
    private $url = 'licensee';

    public function executeIndex(sfWebRequest $request) {
        parent::executeIndex($request);
        $this->showBreadCrumb();
    }

    public function executeNew(sfWebRequest $request) {
        parent::executeNew($request);
        $this->showBreadCrumb($this->actionNewBC);
    }

    public function executeCreate(sfWebRequest $request) {
        parent::executeCreate($request);
        $this->showBreadCrumb($this->actionNewBC);
    }

    public function executeEdit(sfWebRequest $request) {
        parent::executeEdit($request);
        $this->showBreadCrumb($this->actionEditBC);
    }

    public function executeUpdate(sfWebRequest $request) {
        parent::executeUpdate($request);
        $this->showBreadCrumb($this->actionEditBC);
    }

    private function showBreadCrumb($action = NULL){
        if(!empty($action)){
            isicsBreadcrumbs::getInstance()->addItem($this->titleBC, $this->getController()->genUrl($this->url) );
            isicsBreadcrumbs::getInstance()->addItem($action, null);
        }else{
            isicsBreadcrumbs::getInstance()->addItem($this->titleBC, null);
        }
    }

    /* Extra functions */

    public function executeListImport(sfWebRequest $request){
        $this->showBreadCrumb($this->actionImportBC);

        $this->form = new LicenseeImportForm();

        $user = sfContext::getInstance()->getUser()->getGuardUser();

        if ($request->isMethod('post')) {
          $values = $request->getParameter($this->form->getName());

          $this->form->bind($values, $request->getFiles($this->form->getName()));
          if($this->form->isValid()){
              $datetime_points = $this->form->getValue('datetime_points');

              $file = $this->form->getValue('file');
              $array_csv = Licensee::parseLicensee($file->getOriginalName());

              $licensees_list = Doctrine::getTable('Licensee')->getLicensees('CPF Aizenay')->fetchArray();

              $club = Doctrine::getTable('Club')->getClub('CPF Aizenay')->fetchOne();

              foreach( $array_csv as $data_licensee ) {
                $id_licensee = null;
                foreach( $licensees_list as $aLicensee ) {
                    if( $aLicensee["licensee_number"] == $data_licensee["licensee_number"] ) {
                        $id_licensee = $aLicensee["id"];
                        break;
                    }
                }
                
                if( !empty( $id_licensee ) ){
                    $licensee = Doctrine::getTable('Licensee')->getCurrentLicensee($id_licensee)->fetchOne();
                } else {
                    $licensee = new Licensee();
                }

                $licensee->setLicenseeNumber($data_licensee["licensee_number"]);
                $licensee->setLastname($data_licensee["lastname"]);
                $licensee->setFirstname($data_licensee["firstname"]);
                $licensee->setCategory($data_licensee["category"]);
                $licensee->setClubId($club['id']);

                $licensee->save();
                $id_licensee = $licensee->getId();

                $dateTimePointYear = date('Y', strtotime($datetime_points));
                $dateTimePointMonth = date('m', strtotime($datetime_points));
                $newDate = new DateTime($dateTimePointYear . '-' . $dateTimePointMonth . '-' . '01');

                $licensee_point = Doctrine::getTable('LicenseePoint')->getLicenseePointFromDatetime($id_licensee, date('Y-m-d', $newDate->getTimestamp()))->fetchOne();

                if(!$licensee_point){
                    $licensee_point = new LicenseePoint();
                    $licensee_point->setLicenseeId($id_licensee);
                    $licensee_point->setCreationUserId($user->getId());
                    $licensee_point->setModificationUserId($user->getId());
                    $licensee_point->setNbPoints($data_licensee["nb_points_fftt"]);
                    $licensee_point->setDatetimePoints(date('Y-m-d', $newDate->getTimestamp()));
                }

                $licensee_point->setNbPointsFftt($data_licensee["nb_points_fftt"]);
                $licensee_point->save();
              }

              $this->redirect('@licensee');
          }
       }
    }

}
